/**
 * // Definition for a Node.
 * function Node(val,left,right,next) {
 *    this.val = val;
 *    this.left = left;
 *    this.right = right;
 *    this.next = next;
 * };
 */
/**
 * @param {Node} root
 * @return {Node}
 */


function Node(val, left, right, next) {
    this.val = val;
    this.left = left;
    this.right = right;
    this.next = next;
};
var connect = function (root) {
    let cur = root;
    while (cur !== null) {
        let pre = new Node();
        let node = pre;
        while (cur !== null) {
            if (cur.left !== null) {
                node.next = cur.left;
                node = node.next;
            }
            if (cur.right !== null) {
                node.next = cur.right;
                node = node.next;
            }
            cur = cur.next;
        }
        cur = pre.next
    }
    return root;
};


let root = {
    val:12,
    left: {
        val: 19,
        left: {
            val: 10
        },
        right: {
            val: 18
        },
        next: null
    },
    right: {
        val:11,
        left: {
            val:80
        },
        right: {
            val: 70
        },
        next: null
    },
    next: null
}

const res = connect(root)
console.log(res);